//
//  BKHeZhaoStep01ViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/4.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHeZhaoStep01ViewController.h"
#import "BKSearchField.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKHeZhaoViewModel.h"
#import "BKHeZhaoStep02ViewController.h"
#import "BKChuangJianViewController.h"

@interface BKHeZhaoStep01ViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet BKSearchField *sheng;
@property (weak, nonatomic) IBOutlet BKSearchField *shi;
@property (weak, nonatomic) IBOutlet BKSearchField *year;

@property (weak, nonatomic) IBOutlet BKSearchField *xian;
@property (weak, nonatomic) IBOutlet BKSearchField *zhongxue;
@property (nonatomic ,copy) NSArray *zhongxueArray;
@property (nonatomic ,copy) NSArray *cityArray;
@property (nonatomic ,copy) NSArray *countryArray;
@property (nonatomic ,copy) NSDictionary *provDic;
@end

@implementation BKHeZhaoStep01ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _provDic =  @{@"不限":@"0",
                  @"北京":@"1",
                                @"天津":@"2",
                                @"上海":@"3",
                                @"重庆":@"4",
                                @"河北":@"5",
                                @"河南":@"6",
                                @"山东":@"7",
                                @"山西":@"8",
                                @"安徽":@"9",
                                @"江西":@"10",
                                @"江苏":@"11",
                                @"浙江":@"12",
                                @"湖北":@"13",
                                @"湖南":@"14",
                                @"广东":@"15",
                                @"广西":@"16",
                                @"云南":@"17",
                                @"贵州":@"18",
                                @"四川":@"19",
                                @"陕西":@"20",
                                @"青海":@"21",
                                @"宁夏":@"22",
                                @"黑龙江":@"23",
                                @"吉林":@"24",
                                @"辽宁":@"25",
                                @"西藏":@"26",
                                @"新疆":@"27",
                                @"内蒙古":@"28",
                                @"海南":@"29",
                                @"福建":@"30",
                                @"甘肃":@"31"};
    
    
    @weakify(self);
    [RACObserve(self, sheng.text) subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&& [x length]>0) {
            self.cityArray = @[];
            self.countryArray = @[];
            self.shi.text = @"";
            self.xian.text = @"";
            self.zhongxue.text = @"";
           
            [BKHeZhaoViewModel cityOfProv:self.provDic[x] success:^(NSArray *array) {
               
                self.cityArray = array;
                NSMutableArray *temp = [NSMutableArray array];
                for (NSDictionary *dic in array) {
                    [temp addObject:dic[@"name"]];
                }
                
                self.shi.colleageArray = array;
                self.shi.flag = 8004;
                
            } failure:^(NSString *message) {
                //提示信息
            }];
            
        }
        
        
    }];
    
    [RACObserve(self, shi.text) subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length]>0) {
           
            self.countryArray = @[];
            
            self.xian.text = @"";
           
            __block id cityid;
            
            [_cityArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                if ([obj[@"name"] isEqualToString:x]) {
                    cityid = obj[@"id"];
                }
            }];
            
            
            [BKHeZhaoViewModel countryOfCity:cityid success:^(NSArray *array) {
                
                self.countryArray = array;
                NSMutableArray *temp = [NSMutableArray array];
                for (NSDictionary *dic in array) {
                    [temp addObject:dic[@"name"]];
                }
                
                self.xian.colleageArray = array;
                self.xian.flag = 8004;
                
            } failure:^(NSString *message) {
                //提示信息
            }];
            
        }
        
        
    }];
    
    [RACObserve(self, xian.text) subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length]>0) {
            
            self.zhongxueArray = @[];
            
            self.zhongxue.text = @"";
            
            __block id countyid;
            
            [_countryArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                if ([obj[@"name"] isEqualToString:x]) {
                    countyid = obj[@"id"];
                }
            }];
            
            
            [BKHeZhaoViewModel zhongxueWithXian:countyid  success:^(NSArray *array) {
                
                self.zhongxueArray = array;
                NSMutableArray *temp = [NSMutableArray array];
                for (NSDictionary *dic in array) {
                    [temp addObject:@{@"name":dic[@"sname"]}];
                }
                
                self.zhongxue.colleageArray = temp;
                self.zhongxue.flag = 8004;
                
            } failure:^(NSString *message) {
                //提示信息
                
                if ([message hasPrefix:@"暂无"]||[message hasPrefix:@"没有"]) {
                    message = @"找不到该县区的中学，去创建属于自己的学校和班级吧！";
                }

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
                [alertView show];
            }];
            
        }
        
        
    }];


    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)p_back:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)p_submit:(id)sender {
    if ([_sheng.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择省份" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if ([_shi.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择市" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    if ([_xian.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择县区" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    if ([_zhongxue.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择中学" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    
    if ([_year.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择届数" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }

    __weak BKHeZhaoStep01ViewController *weakself = self;
    
    __block id countyid;
    [_countryArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        if ([obj[@"name"] isEqualToString:weakself.xian.text]) {
            countyid = obj[@"id"];
        }
    }];

    
    __block id sid;
    [_zhongxueArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
       
        if ([obj[@"sname"] isEqualToString:weakself.zhongxue.text]) {
            sid = obj[@"sid"];
        }
    }];
    
    
    [BKHeZhaoViewModel banjiWithParams:@{@"sid":sid,
                                         @"page":@"1",
                                        @"len":@"100000",
                                         @"year":_year.text
}
                               success:^(NSArray *array) {
                                   
                                   __block id cityid;
                                   
                                   [weakself.cityArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                                       if ([obj[@"name"] isEqualToString:weakself.shi.text]) {
                                           cityid = obj[@"id"];
                                       }
                                   }];

                                   
                                   NSDictionary *params = @{@"provid":weakself.provDic[weakself.sheng.text],
                                                            @"cityid":cityid,
                                                            @"countyid":countyid,
                                                            @"sid":sid
                                                           };
                                   
                                   BKHeZhaoStep02ViewController *step02 = [[BKHeZhaoStep02ViewController alloc] initWithNibName:NSStringFromClass([BKHeZhaoStep02ViewController class]) bundle:nil];
                                   
                                   step02.params = params;
                                   
                                   step02.dataArray =array;
                                   [self presentViewController:step02 animated:YES completion:nil];
                                   
                                   
                               } failure:^(NSString *message) {
                               
                                   if ([message isEqualToString:@"没有了"]) {
                                       message = @"找不到该县区的中学，去创建属于自己的学校和班级吧！";
                                   }
                                   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                                   [alertView show];
                                   
                                   
                               }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    switch (buttonIndex) {
        case 0:
            
            break;
        case 1:
            if ([alertView.message hasPrefix:@"找不到"]) {
                [alertView setHidden:YES];
                __weak BKHeZhaoStep01ViewController *weakself = self;
                
                __block id cityid;
                [self.cityArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    if ([obj[@"name"] isEqualToString:weakself.shi.text]) {
                        cityid = obj[@"id"];
                    }
                }];
                
                __block id countyid;
                [_countryArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    
                    if ([obj[@"name"] isEqualToString:weakself.xian.text]) {
                        countyid = obj[@"id"];
                    }
                }];
                
                NSDictionary *params = @{@"provid":self.provDic[weakself.sheng.text],
                                         @"cityid":cityid,
                                         @"countyid":countyid,
                                         };

                
                
                BKChuangJianViewController *chuangjian = [[BKChuangJianViewController alloc] initWithNibName:NSStringFromClass([BKChuangJianViewController class]) bundle:nil];
                chuangjian.params = params;
                
                
                [self presentViewController:chuangjian animated:YES completion:nil];
                
            }
        default:
            break;
    }
    [alertView setHidden:YES];
    
}
@end
